package com.jackrain.nea.ad.mapper;

import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.jdbc.SQL;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

@Mapper
public interface AdmonitorMapper {
    class AdmonitorProvider {
        public String update(final JSONObject jsonObject) {
            return new SQL() {
                {
                    Set<String> keySet = jsonObject.keySet();
                    UPDATE("AD_MONITOR");
                    for (int i = 0; i < jsonObject.size(); i++) {
                        String key = (String) keySet.toArray()[i];
                        if ("ID".equals(key)) {
                            WHERE("ID=#{" + key + "}");
                        } else {
                            SET(key + "= #{" + key + "}");
                        }
                    }
                }
            }.toString();
        }

        public String insert(final JSONObject jsonObject) {
            return new SQL() {
                {
                    Set<String> keySet = jsonObject.keySet();
                    INSERT_INTO("AD_MONITOR");
                    for (int i = 0; i < jsonObject.size(); i++) {
                        String key = (String) keySet.toArray()[i];
                        VALUES(key, "#{" + key + "}");
                    }
                }
            }.toString();
        }
    }

    @InsertProvider(type = AdmonitorProvider.class, method = "insert")
    int insertMonitor(JSONObject jsonObject);

    @InsertProvider(type = AdmonitorProvider.class, method = "update")
    int updateMonitor(JSONObject jsonObject);

    @Select("SELECT AD_MENU_ID,NAME,PROPS,ISACTIVE FROM AD_MONITOR WHERE ID=#{id}")
    HashMap selectMonitorInfo(Long id);

    @Select("SELECT AD_MENU_ID,NAME,PROPS,ISACTIVE,ID FROM AD_MONITOR WHERE AD_MENU_ID=#{ad_menu_id}")
    List<HashMap> selectByMenu(Long ad_menu_id);

    @Select("SELECT AD_MENU_ID FROM AD_MONITOR WHERE ID=#{id}")
    Long selectAdmenuid(Long id);

    @Select("SELECT TYPE,AD_TABLE_ID,AD_ACTION_ID,AD_TABLECATEGORY_ID FROM AD_MENU WHERE ID=#{id}")
    HashMap selectMenuType(Long id);

    @Select("SELECT NAME FROM AD_TABLE WHERE ID=#{id}")
    String selectTableName(Long id);

    @Select("SELECT NAME FROM AD_ACTION WHERE ID=#{id}")
    String selectActionName(Long id);

    @Select("SELECT NAME FROM AD_ACTION WHERE ID=#{id}")
    String selectCategoryName(Long id);

    @Select("SELECT COUNT(1) FROM AD_MONITOR WHERE NAME=#{name}")
    int selectName(String name);

    @Select("SELECT COUNT(1) FROM AD_MONITOR WHERE ID=#{id}")
    int idisexist(long id);

    @Select("SELECT NAME FROM AD_MONITOR WHERE ID=#{id}")
    String selectNameById(Long id);
    //查询程序
//    @Select("SELECT a.AD_MENU_ID,a.NAME,a.PROPS,a.ISACTIVE,b.TYPE,b.AD_TABLE_ID,b.AD_TABLECATEGORY_ID FROM AD_MONITOR a LEFT JOIN AD_MENU b ON a.AD_MENU_ID=b.ID WHERE a.ID=#{id}")
//    HashMap selectMonitorAndMenuInfo(Long id);

    @Select("SELECT MASK FROM AD_TABLE WHERE ID=#{id}")
    String selectTableMask(Long id);

    //查询动作定义且不是标签的数据
    @Select("SELECT DESCRIPTION,ID FROM AD_ACTION WHERE AD_TABLE_ID=#{ad_table_id} AND VUEDISPALY!='navbar' AND DISPLAYTYPE!='accord' AND ISACTIVE='Y'")
    List<HashMap> selectActionInfo(Long ad_table_id);

    //查询所有的动作定义
    @Select("SELECT DESCRIPTION,ID FROM AD_ACTION WHERE AD_TABLE_ID=#{ad_table_id} AND DISPLAYTYPE!='accord' AND ISACTIVE='Y'")
    List<HashMap> selectAllActionInfo(Long ad_table_id);

    //查询是否该其他动作定义在该动作定义下
    @Select("SELECT  DESCRIPTION,ID FROM AD_ACTION WHERE AD_ACTION_ID=#{ad_action_id} AND VUEDISPALY!='navbar' AND DISPLAYTYPE!='accord' AND ISACTIVE='Y'")
    List<HashMap> selectActionChildren(Long ad_action_id);

    //查询所有动作定义包含标签
    @Select("SELECT  DESCRIPTION,ID FROM AD_ACTION WHERE AD_ACTION_ID=#{ad_action_id} AND DISPLAYTYPE!='accord' AND ISACTIVE='Y'")
    List<HashMap> selectActionByActionId(Long ad_action_id);

    //查询action的表类别
    @Select("SELECT AD_TABLECATEGORY_ID,AD_TABLE_ID FROM AD_ACTION WHERE ID=#{id}")
    HashMap selectCategoryOrTableid(Long id);

    //查询所有动作定义包含标签
    @Select("SELECT ID,DESCRIPTION FROM AD_ACTION WHERE AD_ACTION_ID=#{ad_action_id} AND VUEDISPALY!='navbar' AND DISPLAYTYPE!='accord' AND ISACTIVE='Y'")
    List<HashMap> slectActionbyActionId(Long ad_action_id);

    //删除
    @Delete("DELETE FROM AD_MONITOR WHERE ID=#{id}")
    int deleeteAction(Long id);

    @Select("SELECT AD_TABLE_ID FROM AD_ACTION WHERE ID=#{id}")
    Long selectactiontableid(Long id);

    @Select("SELECT AD_MENU_ID FROM AD_MONITOR GROUP BY AD_MENU_ID")
    List<Long> admenulist();
}
